package com.qc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qc.common.StringUtil;
import com.qc.dao.SystemMessageDao;
import com.qc.entity.SystemMessageEntity;

@SuppressWarnings({ "rawtypes", "unchecked" })
@Repository("systemMessageDao")
public class SystemMessageDaoImpl implements SystemMessageDao{

	@Resource(name="jdbcTemplate")
	private  JdbcTemplate jdbcTemplate;
	
	@Override
	public int saveSystemMessage(SystemMessageEntity messageEntity) {

		String sql = "insert into t_system_message(user_id,content,system_type) values(?,?,?)";
		
		return jdbcTemplate.update(sql,new Object[]{
				messageEntity.getUserId(),messageEntity.getContent(),messageEntity.getSystemType()});
	}

	@Override
	public List<SystemMessageEntity> listAllSystemMessage(String createdTime,long userId) {
		
		String sql = "select * from t_system_message where 1 = 1 ";
		
		if (StringUtil.isNotEmpty(createdTime)) {
			sql += " and created_time >'"+createdTime+"'";
			
			sql += " and (user_id = "+userId+" or user_id = 0 ) ";
			sql += " order by created_time DESC ";
		}else {
			
			sql += " and (user_id = "+userId+" or user_id = 0 ) ";
			sql += " order by created_time DESC ";
			sql += " LIMIT 100";
		}
		
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(SystemMessageEntity.class));
	}

}
